package com.itboye.ihomebank.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.hetong.ActivityHuaBan;
import com.itboye.ihomebank.activity.hetong.TianXieHeTongActivityZuKe;
import com.itboye.ihomebank.activity.home.ActivityXuanZuKe;
import com.itboye.ihomebank.activity.qianyue.OrderDetailActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.ListDialog;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import static com.itboye.ihomebank.base.BaseOtherActivity.EVENT_TYPE_UNKNOWN;

/**
 * Created by Administrator on 2018/3/5 0005.
 */

public class ListDialogAdapter extends RecyclerView.Adapter<ListDialogAdapter.MyViewHolder> implements Observer {
    Context context;
    LayoutInflater inflater;
    List<String> list;
    List<String> contract_no;
    UserPresenter userPresenter;
    int type; //1  尚有租金逾期未交  2 尚有附件清单未确认 3 尚有未完成的签约 4 尚有申请签约的房源
    public ListDialogAdapter(Context context, List<String> list, int type){
        this.context=context;
        this.list=list;
        this.type=type;
        inflater=LayoutInflater.from(context);
        userPresenter=new UserPresenter(this);
    }
    public ListDialogAdapter(Context context, List<String> list, int type,List<String> contract_no){
        this.context=context;
        this.list=list;
        this.type=type;
        this.contract_no = contract_no;
        inflater=LayoutInflater.from(context);
        userPresenter=new UserPresenter(this);
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate;
        if (type ==2){
             inflate = inflater.inflate(R.layout.item_fjd_list, null, false);
        }else {
             inflate = inflater.inflate(R.layout.item_dialoglist, null, false);
        }
//.setBackground(context.getResources().getDrawable(R.drawable.shape_overdue_gray));  shape_blue05

        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final String[] split = list.get(position).split(",");
        if (type==2){
            holder.tet_content.setText(list.get(position));
            holder.tet_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    userPresenter.querenZichan( SPUtils.get(context, null, SPContants.USER_ID, "") + "",contract_no.get(position),"0");

//                    holder.rl_cancel.setVisibility(View.GONE);
                    holder.tet_ok.setBackground(context.getResources().getDrawable(R.drawable.shape_overdue_gray));
                    holder.tet_ok.setFocusable(false);
                    holder.tet_cancel.setBackground(context.getResources().getDrawable(R.drawable.shape_overdue_gray));
                    holder.tet_cancel.setFocusable(false);

                }
            });
            holder.tet_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    userPresenter.querenZichan( SPUtils.get(context, null, SPContants.USER_ID, "") + "",contract_no.get(position),"1");
//                    holder.rl_ok.setVisibility(View.GONE);
                    holder.tet_ok.setBackground(context.getResources().getDrawable(R.drawable.shape_overdue_gray));
                    holder.tet_ok.setFocusable(false);
                    holder.tet_cancel.setBackground(context.getResources().getDrawable(R.drawable.shape_overdue_gray));
                    holder.tet_cancel.setFocusable(false);
                }
            });
        }else {
            holder.item_left.setText(list.get(position));
            setRightText(holder.item_right,type);
            holder.item_right.setTextColor(context.getResources().getColor(R.color.touzi_blue));
            //确认
            holder.item_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                holder.item_right.setTextColor(context.getResources().getColor(R.color.sixblack));

                    setRightTextClick((TextView) v,type,split[0]);
                }
            });
        }

    }

    public void setRightText(TextView view,int type){
        switch (type){
            case 1:
                view.setText("去交租");
                break;
            case 3:
                view.setText("去签约");
                break;
            case 4:
                view.setText("去选择");
                break;
            default:
                break;
        }
    }

    public void setRightTextClick(TextView view,int type,String house_no){

        Intent intent=null;
        switch (type){
            case 1:
                intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra("contractNo",house_no);
                context.startActivity(intent);
                MyApplcation.mYuQi.cancel();
                MyApplcation.mYuQi=null;
                break;
//            case 2:
//                view.setText("已确认");
//                view.setTextColor(context.getResources().getColor(R.color.sixblack));
//                userPresenter.querenZichan( SPUtils.get(context, null, SPContants.USER_ID, "") + "",house_no,"0");
//                break;
            case 3:
                intent = new Intent(context, ActivityHuaBan.class);
                intent.putExtra("contractNo",house_no);
                context.startActivity(intent);
                MyApplcation.mQianDing.cancel();
                MyApplcation.mQianDing=null;
                break;
            case 4:
                intent = new Intent(context, ActivityXuanZuKe.class);
                intent.putExtra("house_no",house_no);
                context.startActivity(intent);
                MyApplcation.mShenQin.cancel();
                MyApplcation.mShenQin=null;
                break;
            default:
                break;
        }
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void update(Observable o, Object arg) {
        // TODO Auto-generated method stub
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.qingdan_success) {
                ByAlert.alert("附件单已确认，及时交租");
            } else {
                ByAlert.alert("请求失败");
            }
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
       TextView item_left,item_right;
       TextView tet_content,tet_ok,tet_cancel;
       RelativeLayout rl_cancel,rl_ok;
        public MyViewHolder(View itemView) {
            super(itemView);
            if (type==2){
                tet_content= (TextView) itemView.findViewById(R.id.tet_content);
                tet_ok= (TextView) itemView.findViewById(R.id.tet_ok);
                tet_cancel= (TextView) itemView.findViewById(R.id.tet_cancel);
                rl_cancel= (RelativeLayout) itemView.findViewById(R.id.rl_cancel);
                rl_ok= (RelativeLayout) itemView.findViewById(R.id.rl_ok);
            }else {
                item_left= (TextView) itemView.findViewById(R.id.item_left);
                item_right= (TextView) itemView.findViewById(R.id.item_right);
            }

        }
    }

    public ResultEntity handlerError(Object data) {
        ResultEntity result = (ResultEntity) data;
        if (result == null) {
            result = new ResultEntity("-1", "数据格式错误!~", data);
            return result;
        }

        if (result.hasError()) {
//            ByAlert.alert(result.getMsg());
            result.setEventType(EVENT_TYPE_UNKNOWN);
            return result;
        }

        return result;
    }
}
